package com.example.config;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.entity.SensitiveWord;
import com.example.service.SensitiveWordService;
import com.github.houbb.sensitive.word.api.IWordAllow;
import com.github.houbb.sensitive.word.api.IWordDeny;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.allow.WordAllows;
import com.github.houbb.sensitive.word.support.deny.WordDenys;

@Configuration
public class SensitiveWordConfig {

    // @Autowired
    // private MyDdWordAllow myDdWordAllow;

    // @Autowired
    // private MyDdWordDeny myDdWordDeny;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Value("${spring.application.include-default-deny:true}")
    private boolean includeDefaultDeny;

    /**
     * 初始化引导类
     * 
     * @return 初始化引导类
     * @since 1.0.0
     */
    @Bean
    public SensitiveWordBs sensitiveWordBs() {
        SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance();
        if (includeDefaultDeny) {
            sensitiveWordBs.wordDeny(WordDenys.chains(WordDenys.defaults(), new IWordDeny() {
                @Override
                public List<String> deny() {
                    List<SensitiveWord> deny = sensitiveWordService.listDeny();
                    return deny.stream().map(SensitiveWord::getWord).collect(Collectors.toList());
                }
            }));
        } else {
            sensitiveWordBs.wordDeny(new IWordDeny() {
                @Override
                public List<String> deny() {
                    List<SensitiveWord> deny = sensitiveWordService.listDeny();
                    return deny.stream().map(SensitiveWord::getWord).collect(Collectors.toList());
                }
            });
        }

        sensitiveWordBs.wordAllow(WordAllows.chains(/* WordAllows.defaults(), */new IWordAllow() {
            @Override
            public List<String> allow() {
                List<SensitiveWord> allow = sensitiveWordService.listAllow();
                return allow.stream().map(SensitiveWord::getWord).collect(Collectors.toList());
            }
        }));

        sensitiveWordBs.init(); // 初始化

        return sensitiveWordBs;
    }

}